package com.project.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.RoleBean;
import com.project.mapper.IPermissionMapper;
import com.project.mapper.IRoleMapper;
import com.project.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 角色实现类
 */
@Service
@Transactional
public class RoleServiceImpl implements IRoleService {

    @Autowired
    private IRoleMapper roleMapper;
    @Autowired
    private IPermissionMapper permissionMapper;

    @Override
    public IPage<RoleBean> findByItem(Integer pageNO) {
        return roleMapper.selectPage(new Page<>(pageNO, 3), null);
    }

    @Override
    public void del(Integer id) {
        roleMapper.del(id);
    }

    @Override
    public void add(RoleBean roleBean, Integer[] permission) {
        roleMapper.insert(roleBean);
        permissionMapper.add(roleBean.getId(), permission);
    }

    @Override
    public RoleBean findById(Integer roleId) {
        return roleMapper.findById(roleId);
    }

    @Override
    public void update(Integer roleId, Integer[] newArray) {
        roleMapper.delByRoleId(roleId);
        permissionMapper.add(roleId,newArray);
    }

    @Override
    public List<RoleBean> findAll() {
        return roleMapper.selectList(null);
    }
}
